package com.hare.harelearn.DataStructure.ArrayAlgorithm.中等;

/**
 * @program: HareLearn
 * @description: 数组中的最长山脉
 * @author: Hare
 * @create: 2025−03-19 20:20
 * @Version 1.0
 **/
public class 数组中的最长山脉 {

    public static void main(String[] args) {
        int[] arr = {1, 1, 0, 0, 1, 0};
        System.out.println(longestMountain(arr));
    }

    /**
     * 数组中的最长山脉
     *
     * @param arr
     * @return
     */
    public static int longestMountain(int[] arr) {
        int n = arr.length;
        if (n < 3) {
            return 0;
        }
        int bigLength = 0;
        int index = 0;
        while (index < n){
            int up = 0;
            int down = 0;
            while (index < n - 1 && arr[index] < arr[index + 1]) {
                up++;
                index++;
            }

            if(up > 0 ){
                while (index < n - 1 && arr[index] > arr[index + 1]) {
                    down++;
                    index++;
                }
            }

            if(up>0 && down > 0){
                bigLength = Math.max(bigLength, up + down + 1);
            }

            if(up == 0){
                index++;
            }
        }

        return bigLength;
    }
}
